<?php

namespace App\Admin\Controllers;

use App\Models\Cash;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;
use App\Tools\MoneyChange;
use Illuminate\Support\Facades\DB;
use App\Admin\Extensions\ExcelExpoter;

class CashController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }
    public function someone(Request $request)
    {
        try{
            return Admin::content(function (Content $content) {

                $content->header('提现记录');
                $content->description('');
                $content->breadcrumb(
                    ['text' => '资金检查', 'url' => '/users/checkfinance'],
                    ['text' => '']
                );
                $content->body($this->gridsomeone());
            });
        }catch (\Exception $e){
            return redirect('/admin/users/checkfinance');
        }
    }
    protected function gridsomeone()
    {
        return Admin::grid(Cash::class, function (Grid $grid) {

            $grid->filter(function($filter){

                // 去掉默认的id过滤器
                $filter->disableIdFilter();

                // 在这里添加字段过滤器
                $filter->like('bank_account', '银行卡号');
                $filter->like('bank', '银行');
            });

            $user_id = \request('user_id');
            $grid->disableCreateButton();
            $grid->disableActions();
            if (!isset($user_id)){
                return redirect('/admin/users/checkfinance');
            }
            // 添加默认查询条件
            $grid->model()->where('user_id',\request('user_id'));

            $grid->id('ID')->sortable();
            $grid->column('users.name','用户名');

            $grid->column('money','金额');
            $grid->column('bank','银行');
            $grid->column('bank_account','银行卡号');
            $grid->column('real_name','持卡人');
            $grid->column('cash_status','审核结果')->display(function ($res){
                if ($res == '0'){
                    return "<span style='color:#beff79'>已提交审核</span>";
                }elseif ($res == '1'){
                    return "<span style='color:#ff000d'>审核成功</span>";
                }elseif ($res == '2'){
                    return "<span style='color:#4b5157'>审核失败</span>";
                }else{
                    return "<span style='color:#0b00ff'>请联系管理员</span>";
                }
            });
            $grid->column('created_at','创建时间');
            $grid->column('updated_at','修改时间');
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $user_id = $actions->getKey();
                $actions->prepend('<a href="/admin/cash/someone?user_id='.$user_id.'"><i class="fa fa-eye">查看</i></a>');
            });
        });
    }
    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Cash::class, function (Grid $grid) {
            $excel = new ExcelExpoter();
            $excel->setAttr(
                ['id' ,'账号','金额','银行','银行卡号','持卡人','审核状态（0已提交审核，1同意，2拒绝）'],
                ['id', 'users.mobile','money','bank','bank_account','real_name','cash_status'],'提现记录'
            );
            $grid->exporter($excel);
            $grid->model()->orderBy('id', 'desc');
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->like('users.mobile', '用户名');
                $filter->like('real_name', '持卡人');
                $filter->like('bank_account', '银行卡号');
                $filter->equal('cash_status','审核状态')->radio([
                    0    => '已提交审核',
                    1    => '同意',
                    2    => '拒绝',
                ]);
                $filter->where(function ($query) {
                    switch ($this->input) {
                        case '0':
                            $query->where('bank','<>','微信')
                                ->Where('bank', '<>', '支付宝');
                            break;
                        case '1':
                            $query->where('bank','=','支付宝');
                            break;
                        case '2':
                            $query->where('bank','=','微信');
                            break;
                    }
                }, '提现类型')->radio([
                    '0' => '银行',
                    '1' => '支付宝',
                    '2' => '微信',
                ]);
            });
            $grid->disableCreateButton();//禁用新增
            $grid->id('ID')->sortable();
            $grid->column('users.mobile','用户名');
            $grid->column('money','金额');
            $grid->column('bank','银行')->display(function ($bank){
                return "<span class='badge bg-teal'>$bank</span>";
            });
            $grid->column('bank_account','银行卡号');
            $grid->column('real_name','持卡人');
            $grid->column('cash_status','审核状态')->display(function ($cash_status){
                switch ($cash_status){
                    case '0':
                        return "<span class='badge bg-yellow-gradient'>已提交审核</span>";
                        break;
                    case '1':
                        return "<span class='badge bg-green-gradient'>同意</span>";
                        break;
                    case '2':
                        return "<span class='badge bg-red'>拒绝</span>";
                        break;
                    default:
                        return "<span class='badge bg-yellow-active'>请联系管理员</span>";
                        break;
                }
            });
            $grid->created_at('提交时间');
            $grid->updated_at('更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Cash::class, function (Form $form) {
            $form->saving(function (Form $form) {
                if ($form->cash_status == 2) {
                    DB::beginTransaction();
                    try {
                        $user = Cash::where('id', $form->model()->id)->first();
                        $data = Cash::where('id', $form->model()->id)->update(['cash_status' => 2]);
                        $moneychange = new MoneyChange();
                        $moneychange = $moneychange->add_money($user->user_id, $user->money, 1, '提现失败', 0);
                        if ($data && $moneychange) {
                            DB::commit();
                            echo "<script> alert('保存成功！');history.go(-2); </script>";
                            die;
                        }
                    }catch (\Exception $e){
                        DB::rollBack();
                        echo "<script> alert('操作失败，请重试！');history.go(-2); </script>";
                        die;
                    }
                }
            });
            $form->display('id', 'ID');
            $form->display('money','金额');
            $form->display('bank','银行');
            $form->display('bank_account','银行卡号');
            $form->display('real_name','持卡人');
            $cash = Cash::where('id',request()->route()->parameters())->first();
            if($cash['cash_status'] == 0){
                $form->radio('cash_status','审核结果')->options(['1'=> '通过',2 => '拒绝']);
            }else{
                if($cash['cash_status']==1){
                    $form->display('status','审核结果')->default("<span style='color:#009900'>同意</span>");
                }else{
                    $form->display('status','审核结果')->default("<span style='color:#cb2027'>拒绝</span>");
                }
            }
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }

    // demo 统计数据
    public function analysis(){
        return Admin::content(function (Content $content) {

            $content->header('提现统计');
            $content->description('具体信息如下');
            $content->body(view('admin.user.analysis'));
        });
    }
}
